package com.departmentstore.service.impl;

import com.departmentstore.dto.DepartmentProductDTO;
import com.departmentstore.entity.DepartmentProduct;
import com.departmentstore.entity.DepartmentProductId;
import com.departmentstore.entity.Department;
import com.departmentstore.entity.Product;
import com.departmentstore.repository.DepartmentProductRepository;
import com.departmentstore.repository.DepartmentRepository;
import com.departmentstore.repository.ProductRepository;
import com.departmentstore.service.DepartmentProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DepartmentProductServiceImpl implements DepartmentProductService {

    private final DepartmentProductRepository departmentProductRepository;
    private final DepartmentRepository departmentRepository;
    private final ProductRepository productRepository;

    @Autowired
    public DepartmentProductServiceImpl(
        DepartmentProductRepository departmentProductRepository,
        DepartmentRepository departmentRepository,
        ProductRepository productRepository
    ) {
        this.departmentProductRepository = departmentProductRepository;
        this.departmentRepository = departmentRepository;
        this.productRepository = productRepository;
    }

    @Override
    public DepartmentProduct createDepartmentProduct(DepartmentProduct departmentProduct) {
        DepartmentProductId id = new DepartmentProductId(
            departmentProduct.getDepartmentId(),
            departmentProduct.getProductId()
        );
        if (departmentProductRepository.existsById(id)) {
            throw new RuntimeException("该部门-商品关联已存在");
        }

        // 获取部门实体
        Department department = departmentRepository.findById(departmentProduct.getDepartmentId())
            .orElseThrow(() -> new RuntimeException("部门不存在"));
        
        // 获取商品实体
        Product product = productRepository.findById(departmentProduct.getProductId())
            .orElseThrow(() -> new RuntimeException("商品不存在"));

        // 设置关联实体
        departmentProduct.setDepartment(department);
        departmentProduct.setProduct(product);

        return departmentProductRepository.save(departmentProduct);
    }

    @Override
    public DepartmentProduct updateDepartmentProduct(DepartmentProduct departmentProduct) {
        DepartmentProductId id = new DepartmentProductId(
            departmentProduct.getDepartmentId(),
            departmentProduct.getProductId()
        );
        if (!departmentProductRepository.existsById(id)) {
            throw new RuntimeException("部门产品关联不存在");
        }
        return departmentProductRepository.save(departmentProduct);
    }

    @Override
    public void deleteDepartmentProduct(String id) {
        String[] parts = id.split("_");
        if (parts.length != 2) {
            throw new RuntimeException("无效的ID格式");
        }
        DepartmentProductId departmentProductId = new DepartmentProductId(parts[0], parts[1]);
        if (!departmentProductRepository.existsById(departmentProductId)) {
            throw new RuntimeException("部门产品关联不存在");
        }
        departmentProductRepository.deleteById(departmentProductId);
    }

    @Override
    @Transactional(readOnly = true)
    public DepartmentProduct getDepartmentProductById(String id) {
        String[] parts = id.split("_");
        if (parts.length != 2) {
            throw new RuntimeException("无效的ID格式");
        }
        DepartmentProductId departmentProductId = new DepartmentProductId(parts[0], parts[1]);
        return departmentProductRepository.findById(departmentProductId)
                .orElseThrow(() -> new RuntimeException("部门产品关联不存在"));
    }

    @Override
    @Transactional(readOnly = true)
    public List<DepartmentProductDTO> getAllDepartmentProducts() {
        return departmentProductRepository.findAll().stream()
                .map(DepartmentProductDTO::new)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public List<DepartmentProduct> getDepartmentProductsByDepartmentId(String departmentId) {
        return departmentProductRepository.findByDepartment(departmentId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<DepartmentProduct> getDepartmentProductsByProductId(String productId) {
        return departmentProductRepository.findByProduct(productId);
    }

    @Override
    @Transactional(readOnly = true)
    public DepartmentProduct getDepartmentProductByDepartmentIdAndProductId(String departmentId, String productId) {
        return departmentProductRepository.findByDepartmentAndProduct(departmentId, productId);
    }
} 